package com.runen.infinitemovement.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.model.entity.InformationListEntity;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class NewColumnAdapter extends RecyclerView.Adapter<NewColumnAdapter.ViewHolder> {
    private Context context;
    private List<InformationListEntity> mDatas;
    private NewConsultAdapter.OnClick onClick;

    public NewColumnAdapter(Context context) {
        this.context = context;
        this.mDatas = new ArrayList<>();
    }

    public void setData(List<InformationListEntity> mDatas) {
        if (mDatas != null) {
            this.mDatas.clear();
            this.mDatas.addAll(mDatas);
            notifyDataSetChanged();
        }
    }

    public void addList(List<InformationListEntity> mDatas) {
        if (mDatas != null) {
            this.mDatas.addAll(mDatas);
            notifyItemRangeInserted(this.mDatas.size() - mDatas.size(), mDatas.size());
        }
    }

    public List<InformationListEntity> getData() {
        return mDatas;
    }

    public void setOnClick(NewConsultAdapter.OnClick onClick) {
        this.onClick = onClick;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_column, null, false);
        NewColumnAdapter.ViewHolder viewHolder = new NewColumnAdapter.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (holder == null) return;
        InformationListEntity item = mDatas.get(position);
        GlideUtils.loadImage(context, item.cover + "?x-oss-process=image/resize,m_fill,w_144,h_144", holder.rci_logo);
        holder.tv_company_activity.setText(item.title);
        holder.tv_see_num.setText(item.readNumber + "浏览");
        holder.tv_comment_num.setText(item.commentNumber + "评论");
        holder.tv_company_type.setText(item.subsidiary);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClick!=null)
                    onClick.OnItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_consult_click;
        TextView tv_company_activity;
        TextView tv_company_type;
        TextView tv_see_num;
        TextView tv_comment_num;
        RoundedCornersImage rci_logo;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_company_activity = itemView.findViewById(R.id.tv_company_activity);
            tv_company_type = itemView.findViewById(R.id.tv_company_type);
            tv_see_num = itemView.findViewById(R.id.tv_see_num);
            tv_comment_num = itemView.findViewById(R.id.tv_comment_num);
            rci_logo = itemView.findViewById(R.id.rci_logo);
        }
    }
}
